package com.tangsm.redis.script.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;

import java.util.List;

/**
 * redis script lua脚本的资源配置类
 * @author tangsm
 */
@Configuration
public class LuaScriptConfig {

    /**
     * 库存扣减脚本实例注册
     * @return RedisScript脚本实例
     */
    @Bean
    public DefaultRedisScript<Boolean> inventoryDeductScript() {
        DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("script/inventory_deduct.lua")));
        redisScript.setResultType(Boolean.class);
        return redisScript;
    }

    /**
     * 铸造累加脚本实例注册
     * @return RedisScript脚本实例
     */
    @Bean
    public DefaultRedisScript<Boolean> castAccrualScript() {
        DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("script/cast_accrual.lua")));
        redisScript.setResultType(Boolean.class);
        return redisScript;
    }

    /**
     * 铸造累加脚本实例注册
     * @return RedisScript脚本实例
     */
    @SuppressWarnings("rawtypes")
    @Bean
    public DefaultRedisScript<List> listRespScript() {
        DefaultRedisScript<List> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("script/redis_script_list_resp.lua")));
        redisScript.setResultType(List.class);
        return redisScript;
    }
}
